<template>
  <el-dialog
    :title="!dataForm.caId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名称" prop="caName">
      <el-input v-model="dataForm.caName" placeholder="分类名称"></el-input>
    </el-form-item>
<!--    <el-form-item label="分类等级" prop="caLevel">-->
<!--      <el-input v-model="dataForm.caLevel" placeholder="分类等级"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="分类热度" prop="caHeat">
      <el-input v-model="dataForm.caHeat" placeholder="分类热度"></el-input>
    </el-form-item>
<!--    <el-form-item label="父id" prop="caParent">-->
<!--      <el-input v-model="dataForm.caParent" placeholder="父id"></el-input>-->
<!--      :props="{ multiple: true, checkStrictly: true }"-->
<!--    </el-form-item>-->
      <div class="block" >
        <el-form-item label="父级名称" prop="caParent" >
          <el-cascader v-model="dataForm.caParents"
            @change="caParentChange"
            :options="options"
            :props="{ optionProps: true, checkStrictly: true, label: 'caName', value: 'caId' }"
            clearable>
          </el-cascader>
        </el-form-item>
      </div>
    <el-form-item label="分类状态" prop="caStatus">
      <el-input v-model="dataForm.caStatus" placeholder="分类状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false; dataForm.caParents = [];">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {get, options} from "axios";

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          caId: 0,
          caName: '',
          caLevel: '',
          caHeat: '',
          caParent: '',
          caStatus: '',
          caParents: []
        },
        optionProps: {
          value: "caId",
          label: "caName",
          children: "children",
        },
        options: [],
        dataRule: {
          caName: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          caLevel: [
            { required: true, message: '分类等级不能为空', trigger: 'blur' }
          ],
          caHeat: [
            { required: true, message: '分类热度不能为空', trigger: 'blur' }
          ],
          caParent: [
            { required: true, message: '父id不能为空', trigger: 'blur' }
          ],
          caStatus: [
            { required: true, message: '分类状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.getNameAndId();
    },
    methods: {
      caParentChange(value) {
        let level = value.length;
        this.dataForm.caLevel = level
        this.dataForm.caParent = value[value.length - 1];
      },
      getNameAndId(){
        console.log("点击了")
        this.$nextTick(() => {
          this.$http({
            url: this.$http.adornUrl(`/fiction/tbcarte/getCarteNameAndId`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            console.log(data)
            if (data && data.code === 200) {
              this.options = data.data
            }
          })
        })
      },
      init (id) {
        this.dataForm.caId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.caId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbcarte/info/${this.dataForm.caId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.caName = data.tbCarte.caName
                // this.dataForm.caLevel = data.tbCarte.caLevel
                this.dataForm.caHeat = data.tbCarte.caHeat
                this.dataForm.caParent = data.tbCarte.caParent
                this.dataForm.caStatus = data.tbCarte.caStatus
                // this.dataForm = {...data.tbCarte};
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbcarte/${!this.dataForm.caId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'caId': this.dataForm.caId || undefined,
                'caName': this.dataForm.caName,
                'caLevel': this.dataForm.caLevel,
                'caHeat': this.dataForm.caHeat,
                'caParent': this.dataForm.caParent,
                'caStatus': this.dataForm.caStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                    this.dataForm.caParents = []
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
